package com.sunxu.ibog.controller;

import com.sunxu.ibog.domain.EsBlog;
import com.sunxu.ibog.repository.EsBlogRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 孙许
 * @date 2018-05-31
 * @description
 * Blog控制器
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private EsBlogRepository esBlogRepository;

    @GetMapping
    public List<EsBlog> list(@RequestParam(value="title") String title,
                             @RequestParam(value="summary") String summary,
                             @RequestParam(value="content") String content,
                             @RequestParam(value="pageIndex", defaultValue = "0") int pageIndex,
                             @RequestParam(value="pageSize", defaultValue = "10") int pageSize) {
        // 数据在Test中初始化
        Pageable pageable = PageRequest.of(pageIndex, pageSize);
        Page<EsBlog> page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title, summary, content, pageable);
        return page.getContent();
    }
}
